package excmy

import (
	"errors"
	"fmt"
	"github.com/xuri/excelize/v2"
)

func (c *Cell) GetStyle() *excelize.Style {
	//if c.StyleInt != 0 {
	//	style, err := c.File.GetStyle(c.StyleInt)
	//	if err != nil {
	//		fmt.Println("c.File.GetStyle(styleInt) err!", err)
	//		return nil
	//	}
	//	return style
	//}
	styleInt, err := c.File.GetCellStyle(c.Sheet.Name, c.Location())
	//fmt.Println("styleInt::", styleInt)
	if err != nil {
		fmt.Println("GetStyle() err!", err)
		return nil
	}

	style, err := c.File.GetStyle(styleInt)
	if err != nil {
		fmt.Println("c.File.GetStyle(styleInt) err!", err)
		return nil
	}

	return style
}

func (c *Cell) SetStyle(newstyle interface{}) error {
	NewStyleInt, err := c.File.NewStyle(newstyle.(*excelize.Style))
	//fmt.Println("NewStyleInt:", NewStyleInt)
	//	aa, bb := c.File.GetCellStyle(c.Sheet.Name, c.Location())
	//fmt.Println("StyleInt:", aa, bb)
	if err != nil {
		//fmt.Println("GetStyle() err!", err)
		return errors.New(fmt.Sprintf("c.File.NewStyle(newstyle.(*excelize.Style)) ERR %s", err))
	}
	//fmt.Println("c.Location()", c.Location())

	err = c.File.SetCellStyle(c.Sheet.Name, c.Location(), c.Location(), NewStyleInt)
	return err
}

func (c *Cell) SetColor(newcolor string) error {
	style := c.GetStyle()

	newSy := style
	newSy.Fill.Color = append(newSy.Fill.Color, newcolor)
	newSy.Fill.Pattern = 1
	//style.Fill.Color[] = newcolor
	err := c.SetStyle(newSy)
	return err
}

func (c *Cell) StyleInt() int {
	style, err := c.File.GetCellStyle(c.Sheet.Name, c.Location())
	if err != nil {
		panic(err)
	}
	return style
}

func (c *Cell) GetColor() string {
	return c.GetStyle().Fill.Color[0]
}
